package com.bawei.live.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bawei.live.databinding.ItemTypeLayoutBinding
import com.bawei.live.entity.TypeEntity

/**
 * @Author: Lin Liu
 * @CreateTime: 2025年02月17日 16:27:34
 * @Description:
 */
class TypeListAdapter constructor(private val list: List<TypeEntity>): RecyclerView.Adapter<TypeListAdapter.ItemViewHolder>() {

    private var itemClickListener: OnItemClickListener? = null

    inner class ItemViewHolder(val dataBinding: ItemTypeLayoutBinding): RecyclerView.ViewHolder(dataBinding.root)

    //设置Item点击事件
    fun setOnItemClickListener(listener: OnItemClickListener){
        itemClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        return ItemViewHolder(ItemTypeLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        var entity = list[position]
        holder.dataBinding
        holder.dataBinding.ivTypeIcon.setImageResource(entity.icon)
        holder.dataBinding.tvTypeName.text = entity.typeName
        //点击事件
        holder.itemView.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
    }

    override fun getItemCount(): Int = list.size

    /**
     * 点击事件接口类
     */
    interface OnItemClickListener {
        fun onItemChildClick(v: View?, position: Int)
    }

}